package aes.general;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Vector;

/**
 * The Class AdminControl. This calss will process everything the admin does.
 * This class is responsible of the functionality of the things.
 * Uses the MVC model.
 */
public class AdminControl {

	
	private static ArrayList<String> req = null;
	private static ArrayList<String> Exams = null;
	private static ArrayList<String> Questions = null;
	private static ArrayList<String> CourseID = null;
	private static Vector<Vector<String>> UserInfo = null;
	private static ArrayList<Integer> grades = null;
	private static ArrayList<String> ExamID = null;
	
	/**
	 * Gets the requests for the admin to approve time extends or decline them
	 * 
	 * @param requests
	 *            the requests
	 * @return the requests
	 */
	public static void getRequests(ArrayList<String> requests) {
		try {
			GuiControlBeta.getClient().openConnection();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Msg request = new Msg(requests, "300");
		GuiControlBeta.getClient().handleMessageFromClientUI(request);
	}
	

	/**
	 * Gets the req.
	 * 
	 * @return the req
	 */
	public static ArrayList<String> getReq(){
		return req;
	}
	
	
	/**
	 * Sets the requests.
	 * 
	 * @param msg
	 *            the new requests
	 */
	@SuppressWarnings("unchecked")
	public static void setRequests(Object msg) {
		req = (ArrayList<String>) ((Msg) msg).getMsg();	
	}
	

	/**
	 * Gets the information about the teachers for the admin to view when viewing statistics.
	 */
	public static void RunAdminFillTeacherComboBox() {
		try {
			GuiControlBeta.getClient().openConnection();
		} catch (IOException e) {
			e.printStackTrace();
		}		
		String query = new String("Select U.ID, U.Name , U.LastName From teacher T join user U on T.TID=U.Id group by T.TID");
		Msg request1 = new Msg(query, "301");
		GuiControlBeta.getClient().handleMessageFromClientUI(request1);		
		}
	
	/**
	 * Gets the subjects for the admin to view when viewing statistics.
	 */
	public static void RunAdminFillSubjectNameComboBox()
	{
		try {
			GuiControlBeta.getClient().openConnection();
		} catch (IOException e) {
			e.printStackTrace();
		}		
		String query = new String("Select S.Sid ,Sname from subjects S group by S.Sid");
		Msg request1 = new Msg(query, "316");
		GuiControlBeta.getClient().handleMessageFromClientUI(request1);		
	}
	

	/**
	 * Run get teacher's exams for the admin to view when viewing statistics.
	 * 
	 * @param teacherId
	 *            the teacher id
	 */
	public static void RunAdminGetTeachExamId(String teacherId) {
		try {
			GuiControlBeta.getClient().openConnection();
		} catch (IOException e) {
			e.printStackTrace();
		}		
		Msg request1 = new Msg(teacherId, "303");
		GuiControlBeta.getClient().handleMessageFromClientUI(request1);		
	}

    /**
	 * Gets exams for admin to view.
	 * 
	 * @param temp
	 *            the temp
	 */
    public static void ViewExams(ArrayList<String> temp)
    {
	    try {
            GuiControlBeta.getClient().openConnection();
	    } catch (IOException e) {
            System.out.println("304 Can Not Open Connection");
                e.printStackTrace();
	    }
	    String rec = new String("Select ExamId,TID,CreationDate,ExamSubject from exam ");
	    Msg request = new Msg(rec,"304");
	    
	    GuiControlBeta.getClient().handleMessageFromClientUI(request);
    }

	/**
	 * Gets the exams.
	 * 
	 * @return the exams
	 */
	public static ArrayList<String> getExams() {
			return Exams;
	}
	
	/**
	 * Sets the exams vector.
	 * 
	 * @param msg
	 *            the new exam vector
	 */
	@SuppressWarnings("unchecked")
	public static void setExamVector(Object msg) {
		Exams = (ArrayList<String>) ((Msg) msg).getMsg();
	}
	
	/**
	 * Gets questions for admin to view.
	 * 
	 * @param temp
	 *            the temp
	 */
	public static void ViewQuestions(ArrayList<String> temp) {
		try {
			GuiControlBeta.getClient().openConnection();
		} catch (IOException e) {
			System.out.println("305 Can't Open Connection");
				e.printStackTrace();
		}
		String rec = new String("Select QId,TId,Subject,Question from questions ");
		Msg request = new Msg(rec,"305");
		GuiControlBeta.getClient().handleMessageFromClientUI(request);
	}

	/**
	 * Sets the questions array.
	 * 
	 * @param msg
	 *            the new question array
	 */
	@SuppressWarnings("unchecked")
	public static void setQuestionArray(Object msg) {
		Questions = (ArrayList<String>) ((Msg) msg).getMsg();
	}
	
	/**
	 * Gets the questions.
	 * 
	 * @return the questions
	 */
	public static ArrayList<String> getQuestions() {
		return Questions;
	}

	/**
	 * Sets the new exam time after the admin hes approved the time extend request.
	 * 
	 * @param extend
	 *            the new new exam time
	 */
	public static void setNewExamTime(ArrayList<String> extend) {
		try {
			GuiControlBeta.getClient().openConnection();
		} catch (IOException e) {
			System.out.println("306 Can't Open Connection");
				e.printStackTrace();
		}
		Msg request = new Msg(extend,"306");
		GuiControlBeta.getClient().handleMessageFromClientUI(request);
	}
	
	/**
	 * Run admin get report for the specifed exam id.
	 * 
	 * @param examId
	 *            the exam id
	 */
	public static void RunAdminGetReportExamId(String examId)	
	{
		try {
			GuiControlBeta.getClient().openConnection();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Msg request1 = new Msg(examId, "312");
		GuiControlBeta.getClient().handleMessageFromClientUI(request1);	
	}
	

	/**
	 * Gets the courses for the admin when viewing the statistics.
	 * 
	 * @param courses
	 *            the courses
	 * @return the courses
	 */
	public static void getCourses(ArrayList<String> courses) {
		try {
			GuiControlBeta.getClient().openConnection();
		} catch (IOException e) {
			System.out.println("307 Can't Open Connection");
				e.printStackTrace();
		}
		Msg request = new Msg(courses,"307");
		GuiControlBeta.getClient().handleMessageFromClientUI(request);	
	}
	
	/**
	 * Run set course id.
	 * 
	 * @param msg
	 *            the msg
	 */
	@SuppressWarnings("unchecked")
	public static void runSetCourseID(Object msg) {
		CourseID = (ArrayList<String>) ((Msg) msg).getMsg();
	}
	
	
	/**
	 * Gets the course id.
	 * 
	 * @return the course id
	 */
	public static ArrayList<String> getCourseID() {
		return CourseID;
	}


	/**
	 * Gets the information about specified students id.
	 * 
	 * @return the students id
	 */
	public static void getStudentsId() {
		try {
			GuiControlBeta.getClient().openConnection();
		} catch (IOException e) {
			System.out.println("320 Can't Open Connection");
				e.printStackTrace();
		}
		String qery = new String("Select U.ID,U.Name,U.LastName from user U,student S where U.ID = S.SID");
		Msg request = new Msg(qery,"320");
		GuiControlBeta.getClient().handleMessageFromClientUI(request);	
	}


	/**
	 * Sets the vector user info.
	 * 
	 * @param msg
	 *            the msg
	 */
	@SuppressWarnings("unchecked")
	public static void SetVectorUserInfo(Object msg) {
		setUserInfo((Vector<Vector<String>>) ((Msg) msg).getMsg());	
	}


	/**
	 * Gets the user info.
	 * 
	 * @return the user info
	 */
	public static Vector<Vector<String>> getUserInfo() {
		return UserInfo;
	}

	
	/**
	 * Sets the user info.
	 * 
	 * @param UserInfo1
	 *            the new user info
	 */
	public static void setUserInfo(Vector<Vector<String>> UserInfo1) {
		UserInfo = UserInfo1;
	}


	/**
	 * Gets the student statistics when the admin is viewing the reports.
	 * 
	 * @param Sid
	 *            the sid
	 * @return the student stat
	 */
	public static void getStudentStat(String Sid) {
		try {
			GuiControlBeta.getClient().openConnection();
		} catch (IOException e) {
			System.out.println("321 Can't Open Connection");
				e.printStackTrace();
		}
		String qery = new String(Sid);
		Msg request = new Msg(qery,"321");
		GuiControlBeta.getClient().handleMessageFromClientUI(request);
	}


	/**
	 * Calculatign student statistics, like average and median for the admin when
	 * viewing the reports.
	 * 
	 * @param grades
	 *            the grades
	 * @return the array list
	 */
	public static ArrayList<Double> CulcStats(ArrayList<Integer> grades) {
		
		int numofgrades = grades.size();
		ArrayList<Double> stats = new ArrayList<Double>();
		double avg;
		double median;
		int sum = 0;
		for(Integer temp:grades){
			sum+=temp;
		}
	    if(sum > 0){
	    avg = (double)sum/numofgrades;
	    
	    Collections.sort(grades);
	    if (grades.size() % 2 == 1){
		median = grades.get((grades.size()+1)/2-1);
		}
		else
		 {
			double lower = grades.get(grades.size()/2-1);
			double upper = grades.get(grades.size()/2);
			median= (lower + upper) / 2.0;	
		 }
	    }
	    else{
	    	avg = 0.0;
	    	median = 0.0;
	    }
	    stats.add(avg);
	    stats.add(median);
	    stats.add((double)numofgrades);
	    return stats;			
	}

	/**
	 * Gets the grade distribution for the reports section.
	 * 
	 * @param grades
	 *            the grades
	 * @return the distribution
	 */
	public static int[] getDistribution(ArrayList<Integer> grades)
	{
		int[] dis = new int[10];
		for(int i=0;i<grades.size();i++)
		{
			if(grades.get(i) <= 100 && grades.get(i) >= 0)
			{
				if(grades.get(i)!=100)
				dis[(grades.get(i)/10)]++;
				else
					dis[9]++;
			}
		}
		return dis;	
	}
	

	/**
	 * Sets the grades statistics for admin's reports.
	 * 
	 * @param msg
	 *            the new ings grades stats
	 */
	public static void setingsGradesStats(Object msg) {
		setGrades((ArrayList<Integer>) ((Msg) msg).getMsg());	
	}


	/**
	 * Gets the grades.
	 * 
	 * @return the grades
	 */
	public static ArrayList<Integer> getGrades() {
		return grades;
	}


	/**
	 * Sets the grades.
	 * 
	 * @param grades
	 *            the new grades
	 */
	public static void setGrades(ArrayList<Integer> grades) {
		AdminControl.grades = grades;
	}


	/**
	 * Gets the teacher id for the admin to choose for. 
	 * 
	 * @return the teacher id
	 */
	public static void getTeacherId() {
		try {
			GuiControlBeta.getClient().openConnection();
		} catch (IOException e) {
			System.out.println("322 Can't Open Connection");
				e.printStackTrace();
		}
		String qery = new String("Select distinct U.ID,U.Name,U.LastName from user U,teacher T where U.ID = .T.TID ");
		Msg request = new Msg(qery,"322");
		GuiControlBeta.getClient().handleMessageFromClientUI(request);	
	}


	/**
	 * Gets the teacher exam info for the admin to see information.
	 * 
	 * @param Tid
	 *            the teacher id
	 * @return the teacher exam info
	 */
	public static void getTeacherExamInfo(String Tid) {
	
		try {
			GuiControlBeta.getClient().openConnection();
		} catch (IOException e) {
			System.out.println("322 Can't Open Connection");
				e.printStackTrace();
		}
		String qery = new String("Select distinct E.ExamId from exam E,finishedexam Fe where E.ExamId = Fe.ExamId and E.TID = "+ Tid);
		Msg request = new Msg(qery,"323");
		GuiControlBeta.getClient().handleMessageFromClientUI(request);	
	}


	/**
	 * Sets the exame id statistics.
	 * 
	 * @param msg
	 *            the new ings exame id stats
	 */
	public static void setingsExameIdStats(Object msg) {
		setExamID((ArrayList<String>) ((Msg) msg).getMsg());	
	}


	/**
	 * Gets the exam id.
	 * 
	 * @return the exam id
	 */
	public static ArrayList<String> getExamID() {
		return ExamID;
	}


	/**
	 * Sets the exam id.
	 * 
	 * @param examID
	 *            the new exam id
	 */
	public static void setExamID(ArrayList<String> examID) {
		ExamID = examID;
	}
	
	
	/**
	 * Gets the teahcer exam statistics for when the admin is viewing the reports.
	 * 
	 * @param Examid
	 *            the exam id
	 * @return the teacer exam stat
	 */
	public static void getTeacerExamStat(String Examid) {
		
		try {
			GuiControlBeta.getClient().openConnection();
		} catch (IOException e) {
			System.out.println("324 Can't Open Connection");
				e.printStackTrace();
		}
		String qery = new String(Examid);
		Msg request = new Msg(qery,"324");
		GuiControlBeta.getClient().handleMessageFromClientUI(request);	
	}


	/**
	 * Gets the course id for the admin to choose from in the reports section.
	 * 
	 * @return the course id
	 */
	public static void getCourseId() {
		
		try {
			GuiControlBeta.getClient().openConnection();
		} catch (IOException e) {
			System.out.println("322 Can't Open Connection");
				e.printStackTrace();
		}
		String qery = new String("Select C.idCourse,C.CourseName from course C ");
		Msg request = new Msg(qery,"325");
		GuiControlBeta.getClient().handleMessageFromClientUI(request);
	}


	/**
	 * Gets the course exam information for the admin to view.
	 * 
	 * @param Cid
	 *            the course id
	 * @return the course exam info
	 */
	public static void getCourseExamInfo(String Cid) {
		try {
			GuiControlBeta.getClient().openConnection();
		} catch (IOException e) {
			System.out.println("324 Can't Open Connection");
				e.printStackTrace();
		}
		String qery = new String(Cid);
		Msg request = new Msg(qery,"326");
		GuiControlBeta.getClient().handleMessageFromClientUI(request);	
	}
	
	
}
	
